package view;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import model.Book;
import model.Sale;
import dao.BookDAO;
import dao.SaleDAO;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class SellDialog extends JDialog {
    private JComboBox<String> bookCombo;
    private JSpinner quantitySpinner;
    private SalesPanel salesPanel;
    private Map<String, Book> bookMap; // 用于存储书名和Book对象的映射
    
    public SellDialog(JFrame parent, SalesPanel salesPanel) {
        super(parent, "新建销售", true);
        this.salesPanel = salesPanel;
        this.bookMap = new HashMap<>();
        
        setLayout(new GridLayout(3, 2, 5, 5));
        
        // 图书选择
        add(new JLabel("选择图书："));
        bookCombo = new JComboBox<>();
        loadBooks();
        add(bookCombo);
        
        // 数量输入
        add(new JLabel("销售数量："));
        SpinnerModel spinnerModel = new SpinnerNumberModel(1, 1, 100, 1);
        quantitySpinner = new JSpinner(spinnerModel);
        add(quantitySpinner);
        
        // 按钮
        JButton confirmBtn = new JButton("确认");
        JButton cancelBtn = new JButton("取消");
        
        confirmBtn.addActionListener(e -> processSale());
        cancelBtn.addActionListener(e -> dispose());
        
        add(confirmBtn);
        add(cancelBtn);
        
        styleComponents();
        
        pack();
        setLocationRelativeTo(parent);
    }
    
    private void loadBooks() {
        BookDAO dao = new BookDAO();
        List<Book> books = dao.getAllBooks();
        bookCombo.removeAllItems();
        bookMap.clear();
        
        for (Book book : books) {
            String displayText = String.format("%s (作者：%s, 价格：%.2f, 库存：%d)", 
                book.getTitle(), book.getAuthor(), book.getPrice(), book.getStock());
            bookCombo.addItem(displayText);
            bookMap.put(displayText, book);
        }
    }
    
    private void processSale() {
        String selectedBookDisplay = (String) bookCombo.getSelectedItem();
        Book selectedBook = bookMap.get(selectedBookDisplay);
        int quantity = (Integer) quantitySpinner.getValue();
        
        if (selectedBook != null) {
            if (quantity > selectedBook.getStock()) {
                JOptionPane.showMessageDialog(this, 
                    "库存不足！当前库存：" + selectedBook.getStock(), 
                    "错误", 
                    JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            double totalPrice = selectedBook.getPrice() * quantity;
            Sale sale = new Sale(0, selectedBook.getId(), selectedBook.getTitle(), 
                               quantity, totalPrice, new Date());
            
            SaleDAO saleDao = new SaleDAO();
            BookDAO bookDao = new BookDAO();
            
            // 开始处理销售
            boolean saleSuccess = saleDao.addSale(sale);
            boolean stockSuccess = bookDao.updateStock(selectedBook.getId(), quantity);
            
            if (saleSuccess && stockSuccess) {
                JOptionPane.showMessageDialog(this, "销售成功！");
                salesPanel.refreshTable();
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "销售失败！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    
    private void styleComponents() {
        // 设置字体
        Font labelFont = new Font("微软雅黑", Font.PLAIN, 14);
        Font inputFont = new Font("微软雅黑", Font.PLAIN, 14);
        
        // 设置组件样式
        bookCombo.setFont(inputFont);
        quantitySpinner.setFont(inputFont);
        
        // 设置标签样式
        for (Component comp : getContentPane().getComponents()) {
            if (comp instanceof JLabel) {
                JLabel label = (JLabel) comp;
                label.setFont(labelFont);
            }
        }
        
        // 设置按钮样式
        for (Component comp : getContentPane().getComponents()) {
            if (comp instanceof JButton) {
                JButton button = (JButton) comp;
                button.setFont(labelFont);
                button.setFocusPainted(false);
                button.setBackground(new Color(59, 89, 152));
                button.setForeground(Color.WHITE);
                button.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
            }
        }
        
        // 设置下拉框边框
        Border border = BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(new Color(204, 204, 204)),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)
        );
        bookCombo.setBorder(border);
        
        // 设置窗口大小
        setPreferredSize(new Dimension(400, 200));
    }
} 